package com.baidu.foodrecipe.ui.bindingAdapters

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.baidu.foodrecipe.R
import com.bumptech.glide.Glide
import org.jsoup.Jsoup
import org.jsoup.safety.Safelist

@BindingAdapter("setImageURL")
fun setImageURL(view: ImageView, url: String){
    Glide.with(view)
        .load(url)
        .into(view)
}

@BindingAdapter("setFilterColor")
fun setFilterColor(view: View, state: Boolean){
    if (view is TextView){
        if (state){
            view.setTextColor(view.context.getColor(R.color.green))
        }else{
            view.setTextColor(view.context.getColor(R.color.darkGray))
        }
    }
    if (view is ImageView){ //确保图片是svg格式
        if (state){
            view.setColorFilter(view.context.getColor(R.color.green))
        }else{
            view.setColorFilter(view.context.getColor(R.color.darkGray))
        }
    }
}

@BindingAdapter("setHtmlText")
fun setHtmlText(view:TextView,text: String){
    view.text = Jsoup.clean(text, Safelist.none())
}